package plp.funcoo.expressions;

import plp.funcoo.environment.compilation.CompilationEnvironment;
import plp.funcoo.environment.execution.ExecutionContext;
import plp.funcoo.environment.execution.ExecutionEnvironment;
import plp.funcoo.exceptions.ClassNotDeclared;
import plp.funcoo.exceptions.MethodAlreadDeclared;
import plp.funcoo.exceptions.MethodNotDeclared;
import plp.funcoo.exceptions.VarAlreadyDeclared;
import plp.funcoo.exceptions.VarNotDeclared;
import plp.funcoo.value.BooleanValue;
import plp.funcoo.value.IntegerValue;

public class ExpLessEquals extends TestingExpression{

	public ExpLessEquals(Expression left, Expression right, String operator) {
		super(left, right, operator);
	}
	public ExpLessEquals(Expression left, Expression right) {
		super(left, right, "<=");

	}
	@Override
	public ExecutionEnvironment evaluate(ExecutionEnvironment envi) throws VarNotDeclared, MethodNotDeclared, VarAlreadyDeclared, MethodAlreadDeclared {
		
		
		envi = getLeft().evaluate(envi); 
		IntegerValue l = (IntegerValue) ((ExecutionContext) envi).getValueAnalyzed();
		envi = getRight().evaluate(envi); 
		IntegerValue r = (IntegerValue) ((ExecutionContext) envi).getValueAnalyzed();
		((ExecutionContext) envi).setValueAnalyzed(new BooleanValue( l.getValue() <= r.getValue() ));
		return envi;
		
	}
	@Override
	protected boolean checkTypeTerminal(CompilationEnvironment envi)
			throws VarNotDeclared, ClassNotDeclared {

		return getLeft().getType(envi).isInteger() && getRight().getType(envi).isInteger() || getLeft().getType(envi).isFloat() && getRight().getType(envi).isFloat() ;
	}

	public Expression makeCopy() {
		return new ExpLessEquals(this.getLeft().makeCopy(), this.getRight().makeCopy());
	}

}
